<!doctype html>
<html>
<head>
	<meta charset="utf-8"><meta name="format-detection" content="telephone=no">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
	<link rel="stylesheet" type="text/css" href="../../lib/css/skin.css"/>
</head>
<body>

<div id="root">
	<div class="app_warp">
		<div class="app_head">
			<span class="left back"></span>
			<span class="name">裁切所选图片</span>
			<span class="right">
				<a href="javascript:;" tapmode onclick="clip_save()">保存</a>
			</span>
		</div>
		<div class="app_main">
			<div class="nocontent animated fadeInUp">
				<img src="../../lib/img/loading.gif" class="minimize" alt="">
				<div class="text minimize">
					图片正在赶来
				</div>
			</div>
		</div>
	</div>
</div>

<script>
	console.log('' +
		'_$.clipImg({' +
		'   pach:@str' +
		'   callName:@function' +
		'})');
	var
		__setting__,__size__,__clipsize__,__imageclip__,
		clip_open = function (pach,call) {
			if (!pach) {
				call({
					status: false,
					data: null,
					msg: '抱歉,没找到要裁切的图片.'
				});
			} else {
				console.log(JSON.stringify(__size__));
				__imageclip__.open({
					rect: __size__,
					srcPath: pach,
					style: {
						mask: 'rgba(255,255,255,0.95)',
						clip: __clipsize__
					},
					mode:'image'
				}, function (ret, err) {
					if (ret) {
						call({
							status: true,
							data: ret,
							msg: 'ok'
						});
					} else {
						call({
							status: false,
							data: err,
							msg: '抱歉,未能正常裁切图片.'
						});
					}
				});
			}
		},
		__err__=function(){
			api.toast({
				msg: '图片保存失败了 -_-!'
			})
		},
		clip_save = function () {
			__imageclip__.save({
				destPath: 'fs://imageClip/',
				copyToAlbum: false,
				quality: 1
			}, function (ret, err) {
				if (ret) {
					if(ret.destPath!==undefined){
						try{
							api.execScript({
								name:__setting__.name,
								script:__setting__.callName+'("'+ret.destPath+'")'
							})
							_$.sleep(function () {
								api.closeWin();
							}, 1000)
						}catch (e){
							api.toast({
								msg: e.message
							})
						}
					}else{
						__err__();
					}
				} else {
					__err__();
				}
			});
		},
		clip_close = function () {
			__imageclip__.close();
		},
		clip_reset = function () {
			__imageclip__.reset();
		},
		appready = function () {
			__setting__ = api.pageParam;
			__imageclip__ = api.require('FNImageClip');
			__size__ = {
				x: 0,
				y: parseInt(api.winHeight - $('.app_main').height()),
				w: parseInt(api.winWidth),
				h: parseInt($('.app_main').height())
			}
			__clipsize__ = {
				w: __size__.w / 1.25,
				h: __size__.w / 1.25,
				x: __size__.w / 10,
				y: __size__.h / 2 - __size__.w / 2.5,
				borderColor: '#09f',
				borderWidth: 2,
				appearance: 'rectangle'
			}
			clip_open(__setting__.pach, function (data) {
				if (data.status !== true) {
					console.log(JSON.stringify(status));
					api.toast({
						msg: status.msg
					})
					clip_close();
					_$.sleep(function () {
						api.closeWin();
					}, 3500)
				}
			});
		}

</script>
<script src="../../lib/js/config.js"></script>
<script src="../../lib/js/data.js"></script>
<script src="../../lib/js/jquery.js"></script>
<script src="../../lib/js/common.js"></script>
</body>
</html>